var selectedFunction;
var response;
var entity;
var http;
var httpsUrl;

function init() {
	http = new Http();
	
	_initHeader();
	_buttonHandler();
	
	if(memoryPreference.contains("httpsUrl") == true)
		httpsUrl = memoryPreference.get("httpsUrl");
	else
		httpsUrl = "https://m.facebook.com/";
	
	selectedFunction = "get";
}

function _buttonHandler() {
	$("#getButton").click(function(e) {
		selectedFunction = "get";
	});
	$("#postButton").click(function(e) {
		selectedFunction = "post";
	});
	$("#postBodyButton").click(function(e) {
		selectedFunction = "postBody";
	});
	$("#requestButton").click(requestStart);
}

function successCallback(http) {
	platformUIComponent.dismissProgressDialog();
	alert("response : " + http.response);
	notifySuccess("request (" + selectedFunction.toUpperCase() + " 방식)");
}

function errorCallback(http) {
	platformUIComponent.dismissProgressDialog();
	notifyError("request", http.errorMessage);
}

/*
 * GET
 */
function get() {
	alert("GET 방식 통신을 시작합니다.");
	
	entity = {
		"url" : httpsUrl,
		"method" : "GET",
		"onBody" : false
	};
}

/*
 * POST
 */
function post() {
	alert("POST 방식 통신을 시작합니다.");
	
	entity = {
		"url" : httpsUrl,
		"method" : "POST",
		"onBody" : false,
		"parameters" : {
			"parameter1" : "value1",
			"parameter2" : "value2"
		}
	};
}

/*
 * POSTBODY
 */
function postbody() {
	alert("POSTBODY 방식 통신을 시작합니다.");
	
	var content = {
		"id" : "test",
		"pw" : "test",
		"timeout" : "100"
	};
	
	entity = {
		"url" : httpsUrl,
		"method" : "POST",
		"onBody" : true,
		"content" : JSON.stringify(content)
	};
}

/*
 * requestStart
 */
function requestStart() {
	var timeout = $("#timeoutInput").val() * 1000;
	
	if(timeout !== "" || timeout !== null || timeout !== undefined)
		http.setTimeout(timeout);
	
	if(selectedFunction == "get")
		get();
	else if(selectedFunction == "post")
		post();
	else
		postbody();
	
    var retconfirm = confirm("Cancel request test를 하시겠습니까?");
	
	commonProgress("Connecting..", true, "requestCancelCallback", "white");
	
	http.request(entity, successCallback, errorCallback);
    
    if(retconfirm == true){
        platformUIComponent.dismissProgressDialog();
        http.cancelRequest();
        notifySuccess("cancelRequest");
    }
}

function requestCancelCallback() {
	platformUIComponent.dismissProgressDialog();
	http.cancelRequest();
	
	notifySuccess("cancelRequest");
}